﻿using huqiang.Framework;
using huqiang.Framework.Http;
using System.Threading.Tasks;

public class AssetsManager
{
    // Use this for initialization
    public static void Start()
    {
#if UNITY_EDITOR
        if (LocalSetting.IsEditor)
            return;
#endif
#if !UNITY_EDITOR && UNITY_ANDROID
        if (LocalSetting.assetsOption != null)
            if (LocalSetting.assetsOption.aab)
                AssetsAction.AddSetp<UnpackStep>().Reset();
#endif
        AssetsAction.AddSetp<CheckAssetsStep>();//检测资源
        AssetsAction.AddSetp<DownloadStep>();//下载资源
        AssetsAction.AddSetp<FileCheckStep>();//检测资源完整性
        //AssetsAction.AddSetp<WaitStep>().WaitTime = 5;
    }

    // Update is called once per frame
    public static void Update()
    {
        AssetsAction.UpdateState();
        //HttpFileManager.Update();
        //LZMAFileManager.Update();
    }
    public static AssetsChecker.AssetsPart[] Parts;
    public static void DownloadRemainAssets()
    {
        //AssetsChecker checker = new AssetsChecker();
        //checker.IgnoreSection = AssetsVersionControl.sec_Public;
        //checker.RootSection = AssetsVersionControl.sec_Assets;
        //checker.CheckAsync(AssetsVersionControl.NetVersion.ini, AssetsVersionControl.BuildInVersion.ini,
        //    HttpFileManager.AssetsPath + "/",  (o) => {
        //        if(o.Parts!=null)
        //        {
        //            string url = LocalSetting.bundleUrl + AssetsAction.AssetsType;
        //            for(int k=0;k<o.Parts.Length;k++)
        //            {
        //                var list = o.Parts[k].Result;
        //                for (int i = 0; i < list.Count; i++)
        //                {
        //                    var kv = list[i];
        //                    string name = list[i].Name;
        //                    var assets = list[i].assets;
        //                    string df = "/" + name;
        //                    for (int j = 0; j < assets.Count; j++)
        //                    {
        //                        HttpFileManager.Download2(assets[j].name, assets[j].value, url + df + "/" + assets[j].name,
        //                            name, null, assets[j].len, LocalSetting.platform + df);
        //                    }
        //                }
        //            }
        //        }
        //    });
        if(Parts!=null)
        {
            HttpManager manager = HFBase.GetModule<HttpManager>();
            string url = LocalSetting.bundleUrl + AssetsAction.AssetsType;
            string folder = LocalSetting.AssetsPath;
            for (int k = 1; k < Parts.Length; k++)
            {
                var list = Parts[k].Result;
                for (int i = 0; i < list.Count; i++)
                {
                    var kv = list[i];
                    string block = list[i].Name;
                    var assets = list[i].assets;
                    string df = "/" + block;
                    string desf = folder + df;
                    for (int j = 0; j < assets.Count; j++)
                    {
                        manager.Download(desf, assets[j].name,  url + df + "/" + assets[j].name, block, assets[j].value, null, assets[j].len);
                    }
                }
            }
        }
    }
}
